#include <vector>
#include <set>
#include <iostream>

using namespace std;

class Solution 
{
public:
    void setZeroes(vector<vector<int>>& matrix) 
    {
        set<int> row;
        set<int> column;
        for(int i = 0; i < matrix.size(); i++)
        {
            for(int j = 0; j < matrix[0].size(); j++)
            {
                if(matrix[i][j] == 0)
                {
                    row.insert(i);
                    column.insert(j);
                }
            }
        }
        for(auto it = row.begin(); it != row.end(); it++)
        {
            for(int i = 0; i < matrix[0].size(); i++)
            {
                matrix[*it][i] = 0;
            }
        }
        for(auto it = column.begin(); it != column.end(); it++)
        {
            for(int i = 0; i < matrix.size(); i++)
            {
                matrix[i][*it] = 0;
            }
        }
    }
};

// 辅助函数：打印矩阵
void printMatrix(const vector<vector<int>>& matrix) 
{
    cout << "[";
    for(int i = 0; i < matrix.size(); i++) 
    {
        cout << "[";
        for(int j = 0; j < matrix[i].size(); j++) 
        {
            cout << matrix[i][j];
            if(j < matrix[i].size() - 1) cout << ",";
        }
        cout << "]";
        if(i < matrix.size() - 1) cout << ",";
    }
    cout << "]" << endl;
}

int main() 
{
    Solution solution;
    
    // 测试案例1: [[1,1,1],[1,0,1],[1,1,1]]
    cout << "测试案例1:" << endl;
    vector<vector<int>> matrix1 = {{1, 1, 1}, {1, 0, 1}, {1, 1, 1}};
    cout << "输入: matrix = ";
    printMatrix(matrix1);
    solution.setZeroes(matrix1);
    cout << "输出: ";
    printMatrix(matrix1);
    cout << "期望: [[1,0,1],[0,0,0],[1,0,1]]" << endl << endl;
    
    // 测试案例2: [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
    cout << "测试案例2:" << endl;
    vector<vector<int>> matrix2 = {{0, 1, 2, 0}, {3, 4, 5, 2}, {1, 3, 1, 5}};
    cout << "输入: matrix = ";
    printMatrix(matrix2);
    solution.setZeroes(matrix2);
    cout << "输出: ";
    printMatrix(matrix2);
    cout << "期望: [[0,0,0,0],[0,4,5,0],[0,3,1,0]]" << endl << endl;
    
    // 测试案例3: 单行矩阵
    cout << "测试案例3 (单行矩阵):" << endl;
    vector<vector<int>> matrix3 = {{1, 2, 3, 4}};
    cout << "输入: matrix = ";
    printMatrix(matrix3);
    solution.setZeroes(matrix3);
    cout << "输出: ";
    printMatrix(matrix3);
    cout << "期望: [[1,2,3,4]] (无0，不变)" << endl << endl;
    
    // 测试案例4: 单列矩阵
    cout << "测试案例4 (单列矩阵):" << endl;
    vector<vector<int>> matrix4 = {{1}, {2}, {0}, {4}};
    cout << "输入: matrix = ";
    printMatrix(matrix4);
    solution.setZeroes(matrix4);
    cout << "输出: ";
    printMatrix(matrix4);
    cout << "期望: [[0],[0],[0],[0]]" << endl << endl;
    
    // 测试案例5: 全零矩阵
    cout << "测试案例5 (全零矩阵):" << endl;
    vector<vector<int>> matrix5 = {{0, 0}, {0, 0}};
    cout << "输入: matrix = ";
    printMatrix(matrix5);
    solution.setZeroes(matrix5);
    cout << "输出: ";
    printMatrix(matrix5);
    cout << "期望: [[0,0],[0,0]] (已经全零)" << endl << endl;
    
    // 测试案例6: 无零矩阵
    cout << "测试案例6 (无零矩阵):" << endl;
    vector<vector<int>> matrix6 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    cout << "输入: matrix = ";
    printMatrix(matrix6);
    solution.setZeroes(matrix6);
    cout << "输出: ";
    printMatrix(matrix6);
    cout << "期望: [[1,2,3],[4,5,6],[7,8,9]] (无0，不变)" << endl << endl;
    
    // 测试案例7: 第一行有零
    cout << "测试案例7 (第一行有零):" << endl;
    vector<vector<int>> matrix7 = {{0, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    cout << "输入: matrix = ";
    printMatrix(matrix7);
    solution.setZeroes(matrix7);
    cout << "输出: ";
    printMatrix(matrix7);
    cout << "期望: [[0,0,0],[0,5,6],[0,8,9]]" << endl << endl;
    
    // 测试案例8: 第一列有零
    cout << "测试案例8 (第一列有零):" << endl;
    vector<vector<int>> matrix8 = {{1, 2, 3}, {0, 5, 6}, {7, 8, 9}};
    cout << "输入: matrix = ";
    printMatrix(matrix8);
    solution.setZeroes(matrix8);
    cout << "输出: ";
    printMatrix(matrix8);
    cout << "期望: [[0,2,3],[0,0,0],[0,8,9]]" << endl << endl;
    
    // 测试案例9: 多个零
    cout << "测试案例9 (多个零):" << endl;
    vector<vector<int>> matrix9 = {{1, 0, 3}, {4, 5, 6}, {0, 8, 9}};
    cout << "输入: matrix = ";
    printMatrix(matrix9);
    solution.setZeroes(matrix9);
    cout << "输出: ";
    printMatrix(matrix9);
    cout << "期望: [[0,0,0],[0,0,6],[0,0,0]]" << endl << endl;
    
    // 测试案例10: 单元素矩阵
    cout << "测试案例10 (单元素矩阵):" << endl;
    vector<vector<int>> matrix10 = {{0, 0, 0, 5}, {4,3,1,4}, {0,1,1,4},{1,2,1,3},{0,0,1,1}};
    cout << "输入: matrix = ";
    printMatrix(matrix10);
    solution.setZeroes(matrix10);
    cout << "输出: ";
    printMatrix(matrix10);
    cout << "期望: [[0]]" << endl << endl;
    
    return 0;
}